﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BussinessLayer;
using DataAccessLayer.Entity;
namespace QLTSLienThong.Form_he_thong
{
    public partial class frmMonThi : Form
    {
        public frmMonThi()
        {
            InitializeComponent();
        }
        bool ThemMonThi;
        MonThiBussiness monThiBussiness = new MonThiBussiness();
        clsMonThi clsMon;
        bool KiemTraTonTaiMa = true;
        object kq;
        private void LoadMonThi()
        {
            dtgMonThi.DataSource = monThiBussiness.LoadMonThi();
            LoadControl();
            KhoaControl();
        }
        private void LoadControl()
        {
            txtGhiChu.DataBindings.Clear();
            txtGhiChu.DataBindings.Add("Text",dtgMonThi.DataSource,"GhiChu");
            txtMaMon.DataBindings.Clear();
            txtMaMon.DataBindings.Add("Text", dtgMonThi.DataSource, "MaMon");
            txtTenMon.DataBindings.Clear();
            txtTenMon.DataBindings.Add("Text", dtgMonThi.DataSource, "TenMon");
        }
        private void btnLuuMon_Click(object sender, EventArgs e)
        {
            if (KiemTraDL())
            {
                if (ThemMonThi)
                {
                    clsMon = new clsMonThi();
                    clsMon.MaMonThi = txtMaMon.Text.Trim();
                    clsMon.TenMonThi = txtTenMon.Text.Trim();
                    clsMon.GhiChu = txtGhiChu.Text.Trim();
                    monThiBussiness.ThemMon(clsMon);
                    MessageBox.Show("Thêm thành công!","Thông báo");
                    LoadMonThi();
                }
                else
                {
                    clsMon = new clsMonThi();
                    clsMon.MaMonThi = txtMaMon.Text.Trim();
                    clsMon.TenMonThi = txtTenMon.Text.Trim();
                    clsMon.GhiChu = txtGhiChu.Text.Trim();
                    monThiBussiness.SuaMon(clsMon);
                    MessageBox.Show("Sửa thành công!", "Thông báo");
                    LoadMonThi();
                }
            }
        }

        private void btnHuyMon_Click(object sender, EventArgs e)
        {
            LoadMonThi();
        }
        private void KhoaControl()
        {
            txtMaMon.ReadOnly = true;
            txtTenMon.ReadOnly = true;
            txtGhiChu.ReadOnly = true;
            btnHuyMon.SendToBack();
            btnLuuMon.SendToBack();
        }
        private void MoControl()
        {
            txtMaMon.ReadOnly = false;
            txtTenMon.ReadOnly = false;
            txtGhiChu.ReadOnly = false;
            btnHuyMon.BringToFront();
            btnLuuMon.BringToFront();
        }
        private void XoaDL()
        {
            txtGhiChu.Clear();
            txtTenMon.Clear();
            txtMaMon.Clear();
        }

        private void btnSuaMon_Click(object sender, EventArgs e)
        {
            ThemMonThi = false;
            MoControl();
        }

        private void btnThemMon_Click(object sender, EventArgs e)
        {
            ThemMonThi = true;
            MoControl();
            txtMaMon.Focus();
            XoaDL();
        }

        private void btnXoaMon_Click(object sender, EventArgs e)
        {
            XoaMon();
        }
        private void XoaMon()
        {
            if (MessageBox.Show("Bạn muốn xóa mã môn: " + txtMaMon.Text.Trim() + "?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                clsMon = new clsMonThi();
                clsMon.MaMonThi = txtMaMon.Text.Trim();
                monThiBussiness.XoaMon(clsMon);
                LoadMonThi();
            }
        }
        private bool KiemTraDL()
        {
            if (txtMaMon.Text.Trim() == "")
            {
                MessageBox.Show("Chưa nhập mã!","Thông báo");
                txtMaMon.Focus();
                return false;
            }
            if (ThemMonThi == false)
            {
                if (txtMaMon.Text.Trim() == dtgMonThi.CurrentRow.Cells["MaMon"].Value.ToString())
                {
                    KiemTraTonTaiMa = false;
                }
            }
            if (KiemTraTonTaiMa == true)
            {
                clsMon = new clsMonThi();
                clsMon.MaMonThi = txtMaMon.Text.Trim();
                kq = monThiBussiness.KiemTraMa(clsMon);
                if (Convert.ToInt32(kq) > 0)
                {
                    MessageBox.Show("Mã này đã tồn tại!", "Thông báo!");
                    txtMaMon.Focus();
                    return false;
                }
            }
            if (txtTenMon.Text.Trim() == "")
            {
                MessageBox.Show("Chưa nhập tên!", "Thông báo");
                txtTenMon.Focus();
                return false;
            }
            return true;
        }

        private void frmMonThi_Load(object sender, EventArgs e)
        {
            LoadMonThi();
        }
    }
}
